package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.clickinterface.OnProductClickListener;
import com.gensdai.leliang.entity.parseBean.ProductInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页功能按钮的适配
 * Created by LT on 2017/6/15.
 */

public class TypeProductAdapterRec extends DelegateAdapter.Adapter<TypeProductAdapterRec.MainViewHolder> {


    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;

    List<ProductInfo> blist;

    OnProductClickListener click;

    public TypeProductAdapterRec(Context context, LayoutHelper layoutHelper, List<ProductInfo> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public TypeProductAdapterRec(Context context, LayoutHelper layoutHelper, List<ProductInfo> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    public void setOnProductClicklistener(OnProductClickListener click) {
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_type_right_product, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, final int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        final ProductInfo info = blist.get(position);
        if(info != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(click != null){
                        click.OnProductClick(info,position);
                    }
                }
            });
        }

//        if(position % 2 == 0){
//            holder.icon.setBackgroundResource(R.color.image_placebg_color);
//        }else{
//            holder.icon.setBackgroundResource(R.color.image_placebg_color2);
//        }

        holder.icon.setBackgroundResource(R.color.image_placebg_color3);
        if (TextUtils.isEmpty(info.getLogo())) {
            Uri uri = Uri.parse("");
            holder.icon.setImageURI(uri);
        } else {
            Uri uri = Uri.parse(info.getLogo());
//            int width = 100, height = 100;
//            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
//                    .setResizeOptions(new ResizeOptions(width, height))
//                    .build();
//            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
//                    .setOldController(holder.icon.getController())
//                    .setImageRequest(request)
//                    .build();
//
//            holder.icon.setController(controller);
            holder.icon.setImageURI(uri);
        }

        holder.title.setText(info.getProductName());
    }

    @Override
    public int getItemCount() {
        return blist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.icon)
        SimpleDraweeView icon;
        @BindView(R.id.title)
        TextView title;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

}
